package com.imooc.controller;

import com.github.pagehelper.PageHelper;
import com.imooc.BO.ShopCartBO;
import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import com.imooc.service.ItemsService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.vo.CommentLevelCountsVO;
import com.imooc.vo.ItemCommentVO;
import com.imooc.vo.ItemInfoVo;
import com.imooc.vo.SearchItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gcq
 * @Create 2022-01-08
 */
@Api(value = "商品接口", tags = {"商品信息展示的相关接口"})
@RestController
@RequestMapping("items")
public class ItemsController extends BaseController{

    @Autowired
    private ItemsService itemsService;

    @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public IMOOCJSONResult info(@ApiParam(name = "itemId",value = "商品Id", required = true)
                                  @PathVariable String itemId)
    {
        if(itemId == null)
        {
            return IMOOCJSONResult.errorMsg(null);
        }
        Items item = itemsService.queryItemById(itemId);
        List<ItemsImg> itemsImgList= itemsService.queryItemImgList(itemId);
        ItemsParam itemsParamList = itemsService.queryItemParam(itemId);
        List<ItemsSpec> itemsSpecsList = itemsService.queryItemSpecList(itemId);

        ItemInfoVo itemInfoVo = new ItemInfoVo(item, itemsImgList, itemsParamList, itemsSpecsList);
        return IMOOCJSONResult.ok(itemInfoVo);
    }

    @ApiOperation(value = "查询商品评价等级", notes = "查询商品评价等级", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public IMOOCJSONResult commentLevel(@ApiParam(name = "itemId",value = "商品Id", required = true)
                                @RequestParam String itemId)
    {
        if(StringUtils.isBlank(itemId))
        {
            return IMOOCJSONResult.errorMsg(null);
        }
        CommentLevelCountsVO countsVO = itemsService.queryCommentCounts(itemId);
        return IMOOCJSONResult.ok(countsVO);
    }

    @ApiOperation(value = "查询商品评价等级", notes = "查询商品评价等级", httpMethod = "GET")
    @GetMapping("/comments")
    public IMOOCJSONResult commentLevel(@ApiParam(name = "itemId",value = "商品Id", required = true)
                                        @RequestParam String itemId,
                                        @ApiParam(name = "level",value = "等级", required = true)
                                        @RequestParam Integer level,
                                        @ApiParam(name = "page",value = "页码", required = false, defaultValue = "1")
                                        @RequestParam Integer page,
                                        @ApiParam(name = "pageSize",value = "页码大小", required = false, defaultValue = "10")
                                        @RequestParam Integer pageSize)
    {
        if (StringUtils.isBlank(itemId)) {
            return IMOOCJSONResult.errorMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        /**
         * TODO
         * 1、返回分页封装
         * 2、接口参数封装
         */
        PageHelper.startPage(page, pageSize);
        List<ItemCommentVO> itemCommentVOList = itemsService.queryPagedComments(itemId, level);
        return IMOOCJSONResult.ok(getPageResult(itemCommentVOList, page));
    }

    @ApiOperation(value = "搜索商品", notes = "搜索商品", httpMethod = "GET")
    @GetMapping("/search")
    public IMOOCJSONResult searchItems(@ApiParam(name = "keywords",value = "关键字", required = true)
                                        @RequestParam String keywords,
                                        @ApiParam(name = "sort",value = "排序条件", required = true)
                                        @RequestParam String sort,
                                        @ApiParam(name = "page",value = "页码", required = false, defaultValue = "1")
                                        @RequestParam Integer page,
                                        @ApiParam(name = "pageSize",value = "页码大小", required = false, defaultValue = "10")
                                        @RequestParam Integer pageSize)
    {
        if (StringUtils.isBlank(keywords)) {
            return IMOOCJSONResult.errorMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        /**
         * TODO
         * 1、返回分页封装
         * 2、接口参数封装
         */
        PageHelper.startPage(page, pageSize);
        List<SearchItemVO> searchItemVOList = itemsService.searchItems(keywords, sort);
        return IMOOCJSONResult.ok(getPageResult(searchItemVOList, page));
    }

    @ApiOperation(value = "通过分类id搜索商品列表", notes = "通过分类id搜索商品列表", httpMethod = "GET")
    @GetMapping("/catItems")
    public IMOOCJSONResult searchType(@ApiParam(name = "catId",value = "三级分类Id", required = true)
                                       @RequestParam String catId,
                                       @ApiParam(name = "sort",value = "排序条件", required = true)
                                       @RequestParam String sort,
                                       @ApiParam(name = "page",value = "页码", required = false, defaultValue = "1")
                                       @RequestParam Integer page,
                                       @ApiParam(name = "pageSize",value = "页码大小", required = false, defaultValue = "10")
                                       @RequestParam Integer pageSize)
    {
        if (StringUtils.isBlank(catId)) {
            return IMOOCJSONResult.errorMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        /**
         * TODO
         * 1、返回分页封装
         * 2、接口参数封装
         */
        PageHelper.startPage(page, pageSize);
        List<SearchItemVO> searchItemVOList = itemsService.searchItemsByThirdCatId(catId, sort);
//        PageInfo<?> pageInfo = new PageInfo<>(searchItemVOList);
//        PagedGridResult grid = new PagedGridResult();
//        grid.setPage(page);
//        grid.setRows(searchItemVOList);
//        grid.setTotal(pageInfo.getPages());
//        grid.setRecords(pageInfo.getTotal());

        return IMOOCJSONResult.ok(getPageResult(searchItemVOList, page));
    }

    @ApiOperation(value = "根据商品规格Ids查找最新的商品数据", notes = "根据商品规格Ids查找最新的商品数据", httpMethod = "GET")
    @GetMapping("/refresh")
    public IMOOCJSONResult refresh(@ApiParam(name = "itemSpecIds",value = "商品规格id", required = true)
                                      @RequestParam String itemSpecIds)
    {
        if (StringUtils.isBlank(itemSpecIds)) {
            return IMOOCJSONResult.ok();
        }

        List<ShopCartBO> list = itemsService.searchItemsBySpecIds(itemSpecIds);

        return IMOOCJSONResult.ok(list);
    }
}